package com.eight.cloud.client.http;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.eight.cloud.client.common.Constant;
import com.eight.cloud.client.config.RequestConfig;
import com.eight.cloud.client.util.ObjectFormat;
import com.eight.cloud.client.util.RSAUtils;
import com.eight.cloud.client.util.SignatureUtil;

import java.io.IOException;
import java.util.Objects;
import java.util.SortedMap;

/**
 * @ClassName: HttpRequest
 * @Author: TXC
 * @Date: 2024-11-25 16:43
 **/
public class HttpRequest {
    private final RequestConfig requestConfig;

    public HttpRequest(RequestConfig requestConfig) {
        this.requestConfig = requestConfig;
    }

    public String post(String url, String body) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String timestamp = String.valueOf(System.currentTimeMillis() / Constant.THOUSAND);
        String nonce = SignatureUtil.getRandomStr(Constant.TEN);
        String data = ObjectFormat.objectToStr(ObjectFormat.strToObject(body, SortedMap.class)) + timestamp + nonce;
                Request request = new Request.Builder()
                .url(requestConfig.getServerUrl() + url)
                .post(RequestBody.create(MediaType.parse("application/json"), body))
                .addHeader("Content-Type", "application/json")
                .addHeader(Constant.TIMESTAMP, timestamp)
                .addHeader(Constant.SECRET_ID, requestConfig.getSecretId())
                .addHeader(Constant.SECRET_KEY, RSAUtils.encrypt(requestConfig.getSecretKey()))
                .addHeader(Constant.NONCE, nonce)
                .addHeader(Constant.SIGN, SignatureUtil.generateSignature(requestConfig.getSecretKey(), data))
                .build();
        return Objects.requireNonNull(client.newCall(request).execute().body()).string();
    }

    public String get(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String timestamp = String.valueOf(System.currentTimeMillis() / Constant.THOUSAND);
        String nonce = SignatureUtil.getRandomStr(Constant.TEN);
        String data = timestamp + nonce;
        Request request = new Request.Builder()
                .url(requestConfig.getServerUrl() + url)
                .get()
                .addHeader("Content-Type", "application/json")
                .addHeader(Constant.TIMESTAMP, timestamp)
                .addHeader(Constant.SECRET_ID, requestConfig.getSecretId())
                .addHeader(Constant.SECRET_KEY, RSAUtils.encrypt(requestConfig.getSecretKey()))
                .addHeader(Constant.NONCE, nonce)
                .addHeader(Constant.SIGN, SignatureUtil.generateSignature(requestConfig.getSecretKey(), data)).build();
        return Objects.requireNonNull(client.newCall(request).execute().body()).string();
    }

    private SortedMap<String, Object> getQueryStringParams(String url) {
        int questionMarkIndex = url.indexOf("?");
        if (questionMarkIndex == -1) {
            return null;
        }
        String queryString = url.substring(url.indexOf("?") + Constant.ONE);
        return SignatureUtil.getUrlParams(queryString);
    }

    public static class Builder {
        private RequestConfig requestConfig;

        public Builder setRequestConfig(RequestConfig requestConfig) {
            this.requestConfig = requestConfig;
            return this;
        }

        public HttpRequest build() {
            return new HttpRequest(requestConfig);
        }
    }
}
